/*
 * ESNumber.java - ScriptME
 * 
 * Copyright (c) 2009 Cesar Henriques <cesar at alttab.com.ar>.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser Public License v2.1
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * 
 * Based on FESI Project
 * 
 * Contributors:
 * 	Jean-Marc Lugrin - initial API and implementation
 * 	Cesar Henriques <cesar at alttab.com.ar> - J2ME Porting and Extensions
 */
package org.scriptme.ecmascript.data;

import org.scriptme.ecmascript.exceptions.EcmaScriptException;
import org.scriptme.ecmascript.interpreter.Evaluator;

/**
 * Implements the number primitive value as a double.
 */
public final class ESNumber extends ESPrimitive {

	// The value
	/** The value. */
	private double value;

	/**
	 * Create a new double with a specific value.
	 * 
	 * @param value
	 *            The (immutable) value
	 */
	public ESNumber(double value) {
		this.value = value;
	}

	// overrides
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.scriptme.ecmascript.data.ESValue#getTypeOf()
	 */
	public int getTypeOf() {
		return EStypeNumber;
	}

	// overrides
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.scriptme.ecmascript.data.ESValue#getTypeofString()
	 */
	public String getTypeofString() {
		return "number";
	}

	// overrides
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.scriptme.ecmascript.data.ESValue#isNumberValue()
	 */
	public boolean isNumberValue() {
		return true;
	}

	// overrides
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.scriptme.ecmascript.data.ESValue#doubleValue()
	 */
	public double doubleValue() {
		return value;
	}

	// overrides
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.scriptme.ecmascript.data.ESValue#booleanValue()
	 */
	public boolean booleanValue() {
		return !(Double.isNaN(value) || value == 0.0);
	}

	// overrides
	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#toString()
	 */
	public String toString() {
		long intValue = (long) value;
		if (((double) intValue) == value) {
			return Long.toString(intValue);
		} else {
			return Double.toString(value);
		}
	}

	// overrides
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.scriptme.ecmascript.data.ESValue#toESObject(org.scriptme.ecmascript.interpreter.Evaluator)
	 */
	public ESValue toESObject(Evaluator evaluator) throws EcmaScriptException {
		NumberPrototype theObject = null;
		ESObject np = evaluator.getNumberPrototype();
		theObject = new NumberPrototype(np, evaluator);
		theObject.value = this;
		return theObject;
	}

	// overrides
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.scriptme.ecmascript.data.ESValue#toESNumber()
	 */
	public ESValue toESNumber() {
		return this;
	}

	// overrides
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.scriptme.ecmascript.data.ESValue#toJavaObject()
	 */
	public Object toJavaObject() {
		long longValue = (long) value;
		Object o = null;
		if (((double) longValue) == value) {
			if (((byte) longValue) == longValue) {
				o = new Byte((byte) longValue);
			} else if (((short) longValue) == longValue) {
				o = new Short((short) longValue);
			} else if (((int) longValue) == longValue) {
				o = new Integer((int) longValue);
			} else {
				o = new Long(longValue);
			}
		} else {
			o = new Double(value);
		}
		return o;
	}

	// overrides
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.scriptme.ecmascript.data.ESValue#toDetailString()
	 */
	public String toDetailString() {
		return "ES:#'" + Double.toString(value) + "'";
	}
}
